package com.mysql.study.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.awt.*;

@Component
public class UtilsTool{

    //生成6位随机数
    public char[] VerifyCode() {
        String chars = "1234567890qwertyuiopasdfghjklzxcvbnm";
        char[] rands = new char[6];
        for (int i = 0; i < 6; i++) {
            int rand = (int)(Math.random()*35);
            rands[i] = chars.charAt(rand);
        }
        return rands;
    }
    //图片
    public void drawBackround(Graphics g, int width, int height) {
        //给背景色
        g.setColor(new Color(0xDCDCDC));
        //绘制一个矩形,从0,0位置开始绘制,设置宽和高
        g.fillRect(0, 0, width, height);
        for(int i = 0; i < 130; i++) {
            int x = (int) (Math.random()*width);
            int y = (int) (Math.random()*height);
            int red = (int) (Math.random()*255);
            int green = (int) (Math.random()*255);
            int blue = (int) (Math.random()*255);
            //设置干扰点的颜色
            g.setColor(new Color(red, green, blue));
            //设置干扰点的位置及画笔大小
            g.drawOval(x, y, 2, 0);
        }
    }
    //实现邮箱验证码的发送
    @Autowired
    private JavaMailSender mailSender;
    public String SendEmailVerifyCode(String email) {
        String resultCode = String.valueOf(VerifyCode());
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom("luoquan327@foxmail.com");
            simpleMailMessage.setTo(email);
            simpleMailMessage.setSubject("System 验证码");
            simpleMailMessage.setText("尊敬的用户:<br /><hr>" +
                    "你的邮箱验证代码为:<h3>" + resultCode + "</h3>请在60s内完成验证!");
            mailSender.send(simpleMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultCode;
    }
}
